package com.runda.system.controller;

import javax.annotation.Resource;

import com.bbjob.dto.Result;
import com.bbjob.model.SysRole;
import com.runda.system.service.RoleService;
import com.runda.util.DataEntity;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("sys/role")
public class RoleController {
	
	@Resource
	private RoleService roleservice;
	
	@RequestMapping(value = "saveRole", method = RequestMethod.POST)
	public Result<String> saveUser(@RequestBody SysRole role) {
		int ok = roleservice.saveRole(role);
		if(ok > 0) {
			return new Result<String>(200, "保存成功");
		}else {
			return new Result<String>(8106,"保存失败");
		}
	}
	
	@RequestMapping(value = "editRole", method = RequestMethod.POST)
	public Result<String> editUser(@RequestBody SysRole role) {
		int ok = roleservice.editRole(role);
		if(ok > 0) {
			return new Result<String>(200, "修改成功");
		}else {
			return new Result<String>(8106,"修改失败");
		}
	}
	
	@RequestMapping(value = "deleteRole", method = RequestMethod.GET)
	public Result<String> deleteUser(Integer id) {
		int ok = roleservice.deleteRole(id);
		if(ok > 0) {
			return new Result<String>(200, "删除成功");
		}else {
			return new Result<String>(8106,"删除失败");
		}
	}
	
	@RequestMapping(value = "getList", method = RequestMethod.GET)
	public DataEntity getList(SysRole role, @RequestParam(required = false, defaultValue = "1") int page,
			@RequestParam(required = false, defaultValue = "10") int rows) {
		return DataEntity.getInstance(roleservice.getlist(role, page, rows));
	}
	
	@RequestMapping(value = "getAll", method = RequestMethod.GET)
	public DataEntity getList() {
		return DataEntity.getInstance(roleservice.getAll());
	}
	
	/**
	 * 给用户赋予角色权限
	 * @param userid
	 * @param roleid
	 * @return
	 */
	@RequestMapping(value = "authMenu", method = RequestMethod.GET)
	public Result<String> authMenu(@RequestParam(required = true) Integer roleid,@RequestParam(required = true) String menuid) {
		int ok = roleservice.setRoleToMenu(roleid, menuid);
		if(ok > 0) {
			return new Result<String>(200, "操作成功");
		}else {
			return new Result<String>(8106,"操作失败");
		}
	}
	
	/**
	 * 角色获取已选中的权限
	 * @param userid
	 * @param roleid
	 * @return
	 */
	@RequestMapping(value = "getRoleMenu", method = RequestMethod.GET)
	public DataEntity getauth(@RequestParam(required = true) Integer roleid) {
		return DataEntity.getInstance(roleservice.getRoleMenu(roleid));
	}
}
